import { DraggingResolveContext, UseDesignerRules } from "../../../designer-canvas/src/composition/types";
import { ComponentSchema } from "../../../designer-canvas/src/types";
import { ElementPropertyConfig } from '../../../designer-canvas/src/composition/entity/property-entity';
import { DgControl } from '../../../designer-canvas/src/composition/dg-control';

export function useDesignerRules(schema: ComponentSchema, parentSchema?: ComponentSchema): UseDesignerRules {
    let propertyConfig = [];
    const savedSchema = schema;
    /**
     * 判断是否可以接收拖拽新增的子级控件
     */
    function canAccepts(draggingContext: DraggingResolveContext): boolean {

        return false;
    }

    function hideNestedPaddingInDesginerView() {
        return true;
    }

    function getBasicPropConfig(schema: any, viewModelId?: string): ElementPropertyConfig {

        return {
            categoryId: 'basic',
            categoryName: '基本信息',
            properties: [
                {
                    propertyID: 'id',
                    propertyName: '标识',
                    propertyType: 'string',
                    description: '组件的id',
                    readonly: true
                },
                {
                    propertyID: 'type',
                    propertyName: '控件类型',
                    propertyType: 'select',
                    description: '组件的类型',
                    iterator: [{ key: schema.type, value: DgControl.Splitter.name }],
                }
            ],
        };
    }

    function getVisiblePropertyEntity(propertyData: any, viewModelId?: string) {
        return {
            propertyID: 'visible',
            propertyName: '是否可见',
            propertyType: 'unity',
            description: '运行时组件是否可见',
            editorParams: {
                // controlName: UniformEditorDataUtil.getControlName(propertyData),
                constType: 'enum',
                editorOptions: {
                    types: ['const', 'variable'],
                    enums: [{ key: true, value: '是' }, { key: false, value: '否' }],
                    // variables: UniformEditorDataUtil.getVariables(viewModelId, this.domService),
                    // getVariables: () => UniformEditorDataUtil.getVariables(viewModelId, this.domService),
                    newVariableType: 'Boolean',
                    newVariablePrefix: 'is'
                },

            }
        };
    }
    function getBehaviorPropConfig(propertyData: any): ElementPropertyConfig {
        const visibleProp = getVisiblePropertyEntity(propertyData);

        return {
            categoryId: 'behavior',
            categoryName: '行为',
            properties: [
                visibleProp,
                {
                    propertyID: 'isScrollspyContainer',
                    propertyName: '被监听滚动的容器',
                    propertyType: 'boolean',
                    description: '是否为被监听页面滚动的容器',
                    defaultValue: false
                },
                {
                    propertyID: 'scrollOffset',
                    propertyName: '节点偏移量（px）',
                    propertyType: 'number',
                    description: '滚动时每个节点的偏移量',
                    decimals: 0,
                    min: 0,
                    defaultValue: 0,
                    visible: propertyData.isScrollspyContainer
                },
                {
                    propertyID: 'isScrollCollapsibleArea',
                    propertyName: '滚动收折父容器',
                    propertyType: 'boolean',
                    description: '是否为滚动收折父容器',
                    defaultValue: false,
                    readonly: propertyData.isScrollArea
                },
                {
                    propertyID: 'isScrollArea',
                    propertyName: '滚动收折父容器下的滚动区域',
                    propertyType: 'boolean',
                    description: '是否为滚动收折父容器下的滚动区域',
                    defaultValue: false,
                    readonly: propertyData.isScrollCollapsibleArea
                }
            ],
            // setPropertyRelates(changeObject: FormPropertyChangeObject, propData, parameters) {
            //     switch (changeObject.propertyID) {
            //     case 'isScrollspyContainer': {
            //         const scrollOffset = properties.find((p: any) => p.propertyID === 'scrollOffset');
            //         if (scrollOffset) {
            //             scrollOffset.visible = changeObject.propertyValue;
            //         }
            //         break;
            //     }
            //     case 'isScrollCollapsibleArea': {
            //         const isScrollArea = properties.find((p: any) => p.propertyID === 'isScrollArea');
            //         if (isScrollArea) {
            //             isScrollArea.readonly = changeObject.propertyValue;
            //             propertyData.isScrollArea = changeObject.propertyValue ? false : propertyData.isScrollArea;
            //             changeObject.relateChangeProps = [{
            //                 propertyID: 'isScrollArea',
            //                 propertyValue: propertyData.isScrollArea
            //             }];
            //         }
            //         break;
            //     }
            //     case 'isScrollArea': {
            //         const isScrollCollapsibleArea = properties.find((p: any) => p.propertyID === 'isScrollCollapsibleArea');
            //         if (isScrollCollapsibleArea) {
            //             isScrollCollapsibleArea.readonly = changeObject.propertyValue;
            //             propertyData.isScrollCollapsibleArea = changeObject.propertyValue ? false : propertyData.isScrollCollapsibleArea;
            //             changeObject.relateChangeProps = [{
            //                 propertyID: 'isScrollCollapsibleArea',
            //                 propertyValue: propertyData.isScrollCollapsibleArea
            //             }];
            //         }
            //         break;
            //     }
            //     }
            // }
        };
    }
    function getPropsConfig(schema: ComponentSchema): ElementPropertyConfig[] {
        propertyConfig = [];

        // 基本信息属性
        const basicPropConfig = getBasicPropConfig(savedSchema);
        propertyConfig.push(basicPropConfig);

        // 行为属性
        const behaviorPropConfig = getBehaviorPropConfig(savedSchema);
        propertyConfig.push(behaviorPropConfig);

        return propertyConfig;
    }

    return {
        canAccepts,
        hideNestedPaddingInDesginerView,
        getPropsConfig
    };
}
